package com.quan.sdk.weixin.tools;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quan.common.utils.HttpClientUtil;
import com.quan.sdk.weixin.common.WeixinContext;
import com.quan.sdk.weixin.model.Message;

/**
 * 公众号群发消息工具类
 * @author yhaoquan
 *
 */
public class MessageTools {
	
	private static Logger logger = LoggerFactory.getLogger(MessageTools.class);
	
	/**
	 * 根据OpenID列表群发【订阅号不可用，服务号认证后可用】
	 * <br>方式：POST
	 * <br>参数：
	 * <br>&nbsp;&nbsp;&nbsp; access_token 全局唯一接口调用凭据
	 */
	public static String MESSAGE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s" ;
	
	/**
	 * 群发预览接口【订阅号与服务号认证后均可用】
	 * <br>方式：POST
	 * <br>参数：
	 * <br>&nbsp;&nbsp;&nbsp; access_token 全局唯一接口调用凭据
	 */
	public static String PREVIEW_MESSAGE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s" ;
	
	public static JSONObject send(Message message, SendType type) {
		logger.info("消息类型：{}", message.getMsgtype());
		logger.info("群发列表：{}", message.getTouser());
		
		String url = null ;
		if(type.equals(SendType.NORMAL)) { //正常发送
			url = String.format(MESSAGE_SEND_URL, WeixinContext.getAccessToken()) ;
		} else { //预览发送
			url = String.format(PREVIEW_MESSAGE_SEND_URL, WeixinContext.getAccessToken()) ;
		}
		
		Map<String, Object> map = new HashMap<String, Object>() ;
		List<String> toUserList = Arrays.asList(message.getTouser().split(","));
		
		map.put("touser", message.isSendType() ? toUserList : message.getTouser()) ;
		map.put("msgtype", message.getMsgtype());
		
		if(message.getMsgtype().equals(MessageType.text.name())) {//文本消息
			Map<String, Object> content = new HashMap<String, Object>() ;
			content.put("content", message.getContent()) ;
			map.put("text", content);
			
		} else if(message.getMsgtype().equals(MessageType.mpnews.name())) {//图文消息
			Map<String, Object> content = new HashMap<String, Object>() ;
			content.put("media_id", message.getMpnewsMediaId()) ;
			map.put("mpnews", content);
			
		}  else if(message.getMsgtype().equals(MessageType.voice.name())) {//语音消息
			Map<String, Object> content = new HashMap<String, Object>() ;
			content.put("media_id", message.getVoiceMediaId()) ;
			map.put("voice", content);
			
		} else if(message.getMsgtype().equals(MessageType.image.name())) {//图片消息
			Map<String, Object> content = new HashMap<String, Object>() ;
			content.put("media_id", message.getImageMediaId()) ;
			map.put("image", content);
		} else if(message.getMsgtype().equals(MessageType.mpvideo.name())) {//视频消息
			
		} else if(message.getMsgtype().equals(MessageType.wxcard.name())) {//卡券
			Map<String, Object> content = new HashMap<String, Object>() ;
			content.put("card_id", message.getWxcardCardId()) ;
			map.put("wxcard", content);
		}
		
		String string = HttpClientUtil.post(url, JSON.toJSONString(map), null);
		JSONObject parseObject = JSON.parseObject(string);
		
		if(type.equals(SendType.NORMAL)) { //正常发送
			if(parseObject.getInteger("errcode") == 0) {
				logger.info("-------------------群发发送成功：{}", parseObject.getString("errmsg"));
			} else {
				logger.info("-------------------群发发送失败：{}", parseObject.toJSONString());
			}
		} else { //预览发送
			if(parseObject.getInteger("errcode") == 0) {
				logger.info("-------------------预览群发发送成功：{}", parseObject.getString("errmsg"));
			} else {
				logger.info("-------------------预览群发发送失败：{}", parseObject.toJSONString());
			}
		}
		
		return JSON.parseObject(string) ;
	}
	
	/**
	 * 群发类型
	 * <br>正常发送
	 * <br>发送预览（只允许一个用户）
	 */
	public enum SendType {
		NORMAL, PREVIEW
	}
	
	/**
	 * 消息类型
	 */
	public enum MessageType {
		mpnews, text, voice, image, mpvideo, wxcard 
	}

}
